///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef __BUOLA_THREADS_CSIMPLETHREAD_H__
#define __BUOLA_THREADS_CSIMPLETHREAD_H__

#include <buola/buola.h>
#include <buola/threads/cthreadmanager.h>

#include <buola/threads/thread.h>

namespace buola {

///\ingroup threads
///@{

////////////////////////////////////////////////////////////////////////////

/// simple thread

/// This class wraps the boost thread class to provide a simple way to
/// create a class with code that is executed in a new thread. Just inherit
/// this class and override the Run() function, which will be executed in
/// a new thread when Launch() is called
///
////////////////////////////////////////////////////////////////////////////

class CSimpleThread : public UUncopiable
{
public:
//! \name constructor and destructor
//@{
    CSimpleThread()
        :   mThread(nullptr),
            mIsRunning(false)
    {
    }

    virtual ~CSimpleThread()
    {
    }
//@}

public:
    //! returns true if the thread is running
    bool IsRunning()        {   return mIsRunning;  }

    //! creates a new thread and calls Run() in it
    void Launch()
    {
        thread lThread(std::bind(&CSimpleThread::DoRun,this));
        get_main_thread_manager().AddThread(lThread);
    }

private:
    //! function that gets called in the new thread
    virtual void Run()=0;

private:
    void DoRun()
    {
        mIsRunning=true;
        Run();
        mIsRunning=false;
    }

private:
    bool mIsRunning;
};

///@}

/*namespace buola*/ }

#endif
